package com.tk.exam.web.admin;


import com.tk.exam.config.properties.SystemProperties;
import com.tk.exam.domain.TkFile;
import com.tk.exam.domain.TkStudent;
import com.tk.exam.http.Response;
import com.tk.exam.interceptor.NoCheckHttpHeader;
import com.tk.exam.service.StudentService;
import com.tk.exam.service.TkFileService;
import com.tk.exam.utils.ExcelUtils;
import com.tk.exam.utils.PageInfo;
import com.tk.exam.vo.StudentVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;


/**
 * @author: xujg
 * @Description: TODO
 * @date: 2019-06-17 11:26
 */
@Slf4j
@RestController
@RequestMapping("/tk/exam/admin")
public class StudentManagement {


    @Autowired
    SystemProperties systemProperties;
    @Autowired
    private StudentService studentService;
    @Autowired
    TkFileService tkFileService;

    @PostMapping(value = "/student/getListWithPage")
    public Response getListWithPage(@RequestBody StudentVo param) {
        com.github.pagehelper.PageInfo<StudentVo> pageInfoHelper = studentService.getListWithPage(param);
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }

    @PostMapping(value = "/student/edit")
    public Response editStudent(@RequestBody StudentVo param) {
        if (StringUtils.isEmpty(param.getId() + "") || StringUtils.isEmpty(param.getPhoneNo()) || StringUtils.isEmpty(param.getStudentName())) {
            return Response.fail("缺少参数");
        }
        int i = studentService.edit(param);
        return i == 1 ? Response.succeed() : Response.fail("编辑考生失败");
    }

    @GetMapping(value = "/student/getListOfClass")
    public Response getListOfClassWithPage(String classId) {
        if (StringUtils.isEmpty(classId)) {
            return Response.fail("缺少参数");
        }
        com.github.pagehelper.PageInfo<TkStudent> pageInfoHelper = studentService.getListOfClassWithPage(classId);
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }


    @GetMapping(value = "/student/downLoadTemplate")
    @NoCheckHttpHeader
    public String downLoadTemplate() {
        int i = studentService.downLoadTemplate();
        return null;
    }

    @PostMapping(value = "/student/delete")
    public Response deleteStudent(String[] studentIds) {
        if(studentIds==null||studentIds.length==0){
            return Response.fail("studentIds不能为空");
        }
        int i = studentService.deleteStudent(studentIds);
        return i == 1 ? Response.succeed() : Response.fail("删除考生失败");
    }


    @PostMapping(value = "/student/import")
    public Response importStudent(@RequestBody HashMap param) {
        String fileId = (String)param.get("fileId");
        String filePath = (String)param.get("filePath");
        String classId = (String)param.get("classId");
        if(StringUtils.isEmpty(classId)){
            return Response.fail("classId不能为空");
        }
        if(StringUtils.isEmpty(fileId)&&StringUtils.isEmpty(filePath)){
            return Response.fail("fileId和filePath必须传其一");
        }

            String absolutePath = "";
            if (StringUtils.isNotEmpty(fileId)) {
                TkFile tkFile = tkFileService.getTkFile(new Integer(fileId));
                absolutePath = tkFile.getFilePath();
            } else if (StringUtils.isNotEmpty(filePath)) {
                absolutePath = filePath;
            }
            if (StringUtils.isEmpty(absolutePath)) {
                return null;
            }
            absolutePath = systemProperties.getFileUploadPath() + File.separator + absolutePath;
            File file = new File(absolutePath);
            //装载流
            InputStream inputStream =null;
        try {
            inputStream = new FileInputStream(file);
            String fileName = file.getName();
            List<List<String>> list = null;
            if (fileName.endsWith(".xls")) {
                list = ExcelUtils.read(inputStream, true);
            } else {
                list = ExcelUtils.read(inputStream, false);
            }
            int i = studentService.importStudent(list, classId);
            return i == 1 ? Response.succeed() : Response.fail("导入学生失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Response.fail("导入失败");
        }finally {
            try {
                if(inputStream!=null){
                    inputStream.close();
                    log.info("关流完毕=-====");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
